package com.duyi.demo.controller;

import com.duyi.demo.bean.DataBean;
import com.duyi.demo.bean.MapBean;
import com.duyi.demo.data.DataHandler;
import com.duyi.demo.service.DataService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ctx
 * @date 2020/2/14
 * @description
 */
@Controller
public class MapController {

    @Autowired
    private DataService dataService;

    @GetMapping("/map")
    public String show(Model model){
        List<DataBean> list = dataService.list();

        List<MapBean> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DataBean dataBean = list.get(i);

            MapBean mapBean = new MapBean(dataBean.getArea(),dataBean.getConfirm());
            result.add(mapBean);
        }
        model.addAttribute("mapData",new Gson().toJson(result));
        return "map";
    }
}
